﻿using INTEX.DataBase;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.Windows;
namespace WPFExcelReport
{
    //
    public class VYGRMGBEVL : ViewModelBase
    {
        public CollectionView vwSTOCK
        {
            get
            {
                return (CollectionView)CollectionViewSource.GetDefaultView(SetData.Tables["ICC_STOCKM"]);
            }
        }

        private DataRowView _dwSTOCK = null;
        public DataRowView dwSTOCK
        {
            get
            {
                return _dwSTOCK;
            }
            set
            {
                _dwSTOCK = value;
                OnPropertyChanged("dwSTOCK", value);
            }
        }
        private bool _IsYGRMGBEVL; public bool IsYGRMGBEVL { get { return _IsYGRMGBEVL; } set { _IsYGRMGBEVL = value; OnPropertyChanged("IsYGRMGBEVL", value); } }
        private bool _IsEdit; public bool IsEdit { get { return _IsEdit; } set { _IsEdit = value; OnPropertyChanged("IsEdit", value); } }
        private string _CheckDate; public string CheckDate { get { return _CheckDate; } set { _CheckDate = value; OnPropertyChanged("CheckDate", value); } }
        private string _SYEAR; public string SYEAR { get { return _SYEAR; } set { _SYEAR = value; OnPropertyChanged("SYEAR", value); } }
        private string _MDATE; public string MDATE { get { return _MDATE; } set { _MDATE = value; OnPropertyChanged("MDATE", value); } }
        private string _SMOTH;
        public string SMOTH
        {
            get { return _SMOTH; }
            set
            {
                _SMOTH = value;
                SDATE = _SYEAR + Helper.Right("00" + _SMOTH, 2) + "21";
                _MDATE = _SYEAR + Helper.Right("00" + _SMOTH, 2);
                if (_SMOTH == "12")
                {
                    EDATE = Convert.ToInt16(SYEAR) - 1 + "01" + "20";
                }
                else
                {
                    EDATE = SYEAR + Helper.Right("00" + (Convert.ToInt16(SMOTH) + 1), 2) + "20";
                }
                OnPropertyChanged("SMOTH", value);
            }
        }

        private string _SDATE;
        public string SDATE
        {
            get { return _SDATE; }
            set
            {
                _SDATE = value;

                if (!value.EndsWith("21"))
                {
                    IsEdit = true;
                    MessageBox.Show("不是21日开始，请手工进行更新操作");
                }
                else
                {
                    IsEdit = false;
                }
                OnPropertyChanged("SDATE", value);
            }
        }
        private string _EDATE;
        public string EDATE
        {
            get { return _EDATE; }
            set
            {
                _EDATE = value;
                if (!value.EndsWith("20"))
                {
                    IsEdit = true;
                    MessageBox.Show("不是20日结束，请手工进行更新操作");
                }
                else
                {
                    IsEdit = false;
                }
                OnPropertyChanged("EDATE", value);
            }
        }

        private string _LO; public string LO { get { return _LO; } set { _LO = value; OnPropertyChanged("LO", value); } }
        private string _LB; public string LB { get { return _LB; } set { _LB = value; OnPropertyChanged("LB", value); } }
        private string _ART; public string ART { get { return _ART; } set { _ART = value; OnPropertyChanged("ART", value); } }
        private string _VAR; public string VAR { get { return _VAR; } set { _VAR = value; OnPropertyChanged("VAR", value); } }
        bool IsFirstRun = true;
        public DelegateCommand QueryCommand { get; set; }
        public DelegateCommand RefreshCommand { get; set; }
        public DelegateCommand PrintCommand { get; set; }
        VICC_STOCK VICC_STOCK = new VICC_STOCK();
        VICC_STOCKM VICC_STOCKM = new VICC_STOCKM();
        VICC_GLLGEX VICC_GLLGEX = new VICC_GLLGEX();
        VICC_GABEW VICC_GABEW = new VICC_GABEW();
        ITXEXRPT ITXEXRPT;
        public VYGRMGBEVL()
            : base()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetData == null)
            {
                SetData = new SETINTEXDB();
            }
            if (SetProd == null)
            {
                SetProd = new SETINTEXDB();
            }
            SetEnv.Merge(VICC_STOCKM.SetEnv);
            SetProd.Merge(VICC_STOCKM.SetProd);
            SetData.Merge(VICC_STOCKM.SetData);
            IsYGRMGBEVL = true;
            IsReport = true;
            IsEdit = false;
            if (IsReport)
            {
                ITXEXRPT = new ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
            }
            if (app.User.Trim() == "ITXV")
            {
                LO = "01";
                LB = "CP";
                CheckDate = "20141216";
                string LastMonth = "", Begin = "", End = "";
                Helper.GetLastMonth(ref LastMonth, ref Begin, ref End);
                SYEAR = Helper.Left(LastMonth, 4);
                SMOTH = Helper.Right(LastMonth, 2);
                if (app.User.Trim() == "ITXV")
                {
                    SMOTH = "01";
                }
            }
            QueryCommand = new DelegateCommand(x => Query());
            RefreshCommand = new DelegateCommand(x => Refresh());
            PrintCommand = new DelegateCommand(x => Print());
        }

        public void Query()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            try
            {
                int days = 1;
                DateTime _sdate = new System.DateTime();
                DateTime _edate = new System.DateTime();
                Helper.GetDateTime(ref _sdate, _SDATE, "");
                Helper.GetDateTime(ref _edate, _EDATE, "");
                days = _edate.Subtract(_sdate).Days;
                SResult = VICC_STOCKM.LoadMStock(MDATE, SDATE, EDATE, LO, LB);
                SetData.Tables["ICC_STOCKM"].Clear();
                SetData.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        } 
        public void Print()
        {
            ITXEXRPT.dsSource = SetData;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = "YGRGBMOTHLY";
            ITXEXRPT.strExcelFile = app.company + @"\YGRSTOCK.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }
        public override void CallbackImpl(IAsyncResult run, DataSet _SResult)
        {
            base.CallbackImpl(run, _SResult);
            EventDispatcher.Instance.RaiseStatus("完成月报，请查询", StatusEventType.StopProgress);
        }
        public void Refresh()
        {
            VICC_STOCKM.RefreshMGB(MDATE, SDATE, EDATE, LO, LB);
            return;
            EventDispatcher.Instance.RaiseStatus("开始更新库存数据", StatusEventType.StartProgress);
            fHandle = new AsyncRunHandle(VICC_STOCKM.RefreshMGB);
            VICC_STOCKM.AsyncRun(fHandle, "RefreshMGB", MDATE, SDATE, EDATE, LO, LB);
        }
    }
}